package com.example.untils;

import cn.hutool.core.util.NumberUtil;
import com.example.entity.money.RaxDto;
import com.example.entity.money.RaxVo;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class MoneyUtils {

    /**
     * 判断字符串是否为正数
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        if(StringUtils.isEmpty(str)) return false;
        // 使用正则表达式判断字符串是否为数字类型
        return str.matches("^[0-9]+(\\.[0-9]+)?$");
    }

    public static RaxVo getRax(RaxDto raxDto) {
        RaxVo vo = new RaxVo();
        BigDecimal price = NumberUtil.mul(raxDto.getNums(), raxDto.getUnitPrice());
        vo.setPrice(NumberUtil.round(price, 6));
        // 1应税内含  2应税外加  3零税率
        switch (raxDto.getType()) {
            case "1":
                vo.setPriceBeforeTax(NumberUtil.div(price, NumberUtil.add(1, raxDto.getRax()), 4, RoundingMode.HALF_UP));
                vo.setTaxPrice(NumberUtil.round(NumberUtil.sub(price, vo.getPriceBeforeTax()), 4));

                vo.setTaxPriceLocal(NumberUtil.round(NumberUtil.mul(vo.getTaxPrice(), raxDto.getExchangeRate()), 4));
                vo.setPriceBeforeTaxLocal(NumberUtil.round(NumberUtil.mul(vo.getPriceBeforeTax(), raxDto.getExchangeRate()), 6));
                vo.setPriceLocal(NumberUtil.round(NumberUtil.add(vo.getTaxPriceLocal(), vo.getPriceBeforeTaxLocal()), 6));
                break;
            case "2":
                vo.setPriceBeforeTax(vo.getPrice());
                vo.setTaxPrice(NumberUtil.round(NumberUtil.mul(raxDto.getRax(), price), 4));

                vo.setTaxPriceLocal(NumberUtil.round(NumberUtil.mul(vo.getTaxPrice(), raxDto.getExchangeRate()), 6));
                vo.setPriceBeforeTaxLocal(NumberUtil.round(NumberUtil.mul(vo.getPriceBeforeTax(), raxDto.getExchangeRate()), 6));
                vo.setPriceLocal(NumberUtil.round(NumberUtil.add(vo.getTaxPriceLocal(), vo.getPriceBeforeTaxLocal()), 6));
                break;
            case "3":
            case "4":
            case "9":
                vo.setPriceBeforeTax(vo.getPrice());
                vo.setTaxPrice(new BigDecimal(0));

                vo.setTaxPriceLocal(new BigDecimal(0));
                vo.setPriceLocal(NumberUtil.round(NumberUtil.mul(vo.getPrice(), raxDto.getExchangeRate()), 6));
                vo.setPriceBeforeTaxLocal(NumberUtil.round(NumberUtil.mul(vo.getPriceBeforeTax(), raxDto.getExchangeRate()), 6));
                break;
        }
//        vo.setPrice(NumberUtil.round(price, 10));
//        vo.setPriceBeforeTax(NumberUtil.div(price, NumberUtil.add(1, raxDto.getRax()), 10, RoundingMode.HALF_UP));
//        vo.setTaxPrice(NumberUtil.round(NumberUtil.sub(price, vo.getPriceBeforeTax()), 10));
//        vo.setPriceLocal(NumberUtil.round(NumberUtil.mul(vo.getPrice(), raxDto.getExchangeRate()), 10));
//        vo.setPriceBeforeTaxLocal(NumberUtil.round(NumberUtil.mul(vo.getPriceBeforeTax(), raxDto.getExchangeRate()), 10));
//        vo.setTaxPriceLocal(NumberUtil.round(NumberUtil.mul(vo.getTaxPrice(), raxDto.getExchangeRate()), 10));
        return vo;
    }

}
